var tempPendding = JSON.parse(localStorage.getItem('pendding'))
var tempFinished = JSON.parse(localStorage.getItem('finished'))



const initialState = {
    penddingList: tempPendding || [],
    finishedList: tempFinished || []
}

export function todoReducer(state = initialState, action) {
    console.log(action)
    switch (action.type) {
        // 添加todo到进行中
        case "add_todo":
            // console.log(1111,)
            var flag = false
            state.penddingList.forEach((ele) => {
                if (action.text === ele.text) {
                    window.alert('请勿添加重复todo')
                    flag = true
                }
            })

            if (!flag) {
                var tempList = {
                    text: action.text,
                    status: "PENDDING"
                }
                console.log(1111, tempList)
                state.penddingList.push(tempList)
            }
            // 存本地
            localStorage.setItem('pendding',JSON.stringify(state.penddingList))
            localStorage.setItem('finished',JSON.stringify(state.finishedList))
            return {
                penddingList: [...state.penddingList],
                finishedList: [...state.finishedList]
            }
        // 从进行中到已完成
        case "pendding_to_finished":
            // console.log(123123)
            state.penddingList.forEach((ele,index)=>{
                if(ele.text === action.todo.text){
                    state.penddingList.splice(index,1)
                }
            })
            action.todo.status = "FINISHED"
            state.finishedList.push(action.todo)
            // 存本地
            localStorage.setItem('pendding',JSON.stringify(state.penddingList))
            localStorage.setItem('finished',JSON.stringify(state.finishedList))

            return {
                penddingList: [...state.penddingList],
                finishedList: [...state.finishedList]
            }
        // 从已完成到进行中
        case "finished_to_pendding":
            state.finishedList.forEach((ele,index)=>{
                if(ele.text === action.todo.text){
                    state.finishedList.splice(index,1)
                }
            })
            action.todo.status = "PENDDING"
            state.penddingList.push(action.todo)

            // 存本地
            localStorage.setItem('pendding',JSON.stringify(state.penddingList))
            localStorage.setItem('finished',JSON.stringify(state.finishedList))
            return {
                penddingList: [...state.penddingList],
                finishedList: [...state.finishedList]
            }
        // 从进行中删除
        case "pendding_delete":
            state.penddingList.forEach((ele,index)=>{
                if(ele.text === action.todo.text){
                    state.penddingList.splice(index,1)
                }
            })
            // 存本地
            localStorage.setItem('pendding',JSON.stringify(state.penddingList))
            localStorage.setItem('finished',JSON.stringify(state.finishedList))
            return {
                penddingList: [...state.penddingList],
                finishedList: [...state.finishedList]
            }
        // 从已完成删除
        case "finished_delete":
            state.finishedList.forEach((ele,index)=>{
                if(ele.text === action.todo.text){
                    state.finishedList.splice(index,1)
                }
            })
            // 存本地
            localStorage.setItem('pendding',JSON.stringify(state.penddingList))
            localStorage.setItem('finished',JSON.stringify(state.finishedList))
            return {
                penddingList: [...state.penddingList],
                finishedList: [...state.finishedList]
            }
        default:
            return state
    }
}